package com.dw.widget.slideshow.config;

import java.util.List;

import com.dw.widget.slideshow.R;
import com.dw.widget.slideshow.config.view.list.BaseListItemView;
import com.dw.widget.slideshow.config.view.list.ListBigIconSelectView;
import com.dw.widget.slideshow.config.view.list.ListBigIconSelectView.OnCheckBoxSwitchListener;
import com.dw.widget.slideshow.utils.AppConfig;
import com.dw.widget.slideshow.utils.ApplicationEx;

import android.content.res.Resources;
import android.os.Bundle;

public class FrameChooserActivity extends BaseBigIconSelectActivity {

	private final ApplicationEx _APP = ApplicationEx.INSTANCE();
	
	public void initListData()
	{
		Resources res = this.getResources();
		listItemData = new Object[][]{{R.drawable.icon_animation01, res.getString(R.string.frame_simple), false},
									  {R.drawable.icon_animation02, res.getString(R.string.frame_simple2), false},
									  {R.drawable.icon_animation03, res.getString(R.string.frame_wood), false},
									  {R.drawable.icon_animation04, res.getString(R.string.frame_stick), false},
									  {R.drawable.icon_animation01, res.getString(R.string.frame_album), false}};

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {

		initListData();
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void initViews() {

		super.initViews();
		header.setTitle(getResources().getString(R.string.choose_style_tip));

		final List<BaseListItemView> listItems = listBlock.getListItems();
		
		// 读取配置项
		final AppConfig appConfig = new AppConfig(FrameChooserActivity.this, _APP.CONFIG_FILE_NAME);
		int selectedFrame = appConfig.getInt(_APP.CONFIG_ITEM_FRAME, _APP.FRAME_STYLE1);
		int selectedIdx = selectedFrame - _APP.FRAME_STYLE1;
		ListBigIconSelectView biv = (ListBigIconSelectView)listItems.get(selectedIdx);
		biv.getCheckBox().setChecked(true);
		
		final int count = listItems.size();
		for(int i = 0; i < count; i++)
		{
			final int index = i;
			BaseListItemView item = listItems.get(i);
			ListBigIconSelectView bigIconView = (ListBigIconSelectView)item;
			bigIconView.setOncheckBoxSwitchListener(new OnCheckBoxSwitchListener() {
				
				@Override
				public void OnSwitch() {
					
					// 把其他设置项设为未选中
					for(int j = 0; j < count; j++)
					{
						ListBigIconSelectView biv = (ListBigIconSelectView)listItems.get(j);
						
						if(j == index)
						{
							biv.getCheckBox().setChecked(true);
							continue;
						}
						
						biv.getCheckBox().setChecked(false);
					}
					
					// 更新配置文件
					int selectedFrame = _APP.FRAME_STYLE1 + index;
					
					final AppConfig appConfig = new AppConfig(FrameChooserActivity.this, _APP.CONFIG_FILE_NAME);
					appConfig.putInt(_APP.CONFIG_ITEM_FRAME, selectedFrame);
					
					// 关闭窗体
					FrameChooserActivity.this.finish();
				}
			});
		}
	}
	
	
}
